package com.inhim.pjhim.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.inhim.pjhim.R;
import com.inhim.pjhim.entity.ProjectListEntity;
import com.inhim.pjhim.entity.ReaderStyle;
import com.inhim.pjhim.entity.ReaderTypeList;
import com.inhim.pjhim.utils.ImageLoaderUtils;

import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;
import org.yczbj.ycrefreshviewlib.holder.BaseViewHolder;

import fm.jiecao.jcvideoplayer_lib.JCMediaManager;
import fm.jiecao.jcvideoplayer_lib.JCUtils;

public class ProjectRadioListAdapter extends RecyclerArrayAdapter<ProjectListEntity.List> {

    private ReaderStyle.List readerStyle;
    private ReaderTypeList.List readerType;
    private Activity context;
    private boolean mIsCheck;
    private int mPlayPosition;
    private boolean mIsFirst;
    public ProjectRadioListAdapter(Activity context) {
        super(context);
        this.context=context;
    }

    public void setFirst(boolean isFirst){
        this.mIsFirst=isFirst;
    }
    public void  setReaderStyle(ReaderStyle.List readerStyle){
        this.readerStyle=readerStyle;
    }
    public void  setReaderType(ReaderTypeList.List readerType){
        this.readerType=readerType;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        PersonViewHolder personViewHolder =new PersonViewHolder(parent,viewType);
        return personViewHolder;
    }

   /* public void setCheck(boolean isCheck) {
        mIsCheck=isCheck;
    }
*/
    public void setPlayPosition(int position,boolean isCheck) {
        mPlayPosition=position;
        mIsCheck=isCheck;
    }

    public class PersonViewHolder extends BaseViewHolder<ProjectListEntity.List> {

        ImageView iv_title;
        TextView tv_title;
        TextView tv_time1;
        TextView tv_content;
        TextView textview2;
        TextView tv_time;
        int mPosition;
        CheckBox iv_icon;
        PersonViewHolder(ViewGroup parent,int position){
            super(parent, R.layout.item_radio_project_list);
            mPosition=position;
            iv_title = getView(R.id.iv_title);
            tv_title = getView(R.id.tv_title);
            tv_time1 = getView(R.id.tv_time1);
            tv_content = getView(R.id.tv_content);
            iv_icon=getView(R.id.iv_icon);
            textview2=getView(R.id.textview2);
            tv_time=getView(R.id.tv_time);
            addOnClickListener(R.id.iv_icon);
        }
        @Override
        public void setData(ProjectListEntity.List data) {
            super.setData(data);
            //tv_content.setText("上传时间:"+data.getCreateTime());
            tv_time1.setText("作者:");
            tv_time1.setVisibility(View.GONE);
            if(data.getAuthor()!=null){
                tv_time1.setText("作者:" + data.getAuthor().getName());
            }
            ImageLoaderUtils.setImage(data.getCover(),iv_title);
            tv_title.setText(data.getTitle());
            textview2.setText(data.getTagName());
            if(data.getTagName()==null || "".equals(data.getTagName())){
                textview2.setVisibility(View.GONE);
            }else{
                textview2.setVisibility(View.VISIBLE);
                textview2.setText(data.getTagName());
            }
            //tv_content.setText("上传时间:"+data.getCreateTime());
            if(data.getDuration()!=null){
                tv_time.setText("时长 "+JCUtils.stringForTime2(Integer.valueOf(data.getDuration())));
            }else{
                tv_time.setText("时长 00:00");
            }
            //1文章 2视频 3音频
            if(!mIsFirst){
                if(mPlayPosition ==mPosition){
                    iv_icon.setChecked(mIsCheck);
                }else{
                    iv_icon.setChecked(false);
                }
            }
            try {
                if(mIsFirst){
                    if(JCMediaManager.instance().mediaPlayer.isPlaying() && JCMediaManager.instance().CURRENT_PLAYING_URL.equals(data.getUrl())){
                        iv_icon.setChecked(true);
                        mIsFirst=false;
                        if(mNotifyPositionListener!=null){
                            mNotifyPositionListener.setPosition(mPosition);
                        }
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    private NotifyPositionListener mNotifyPositionListener;

    public void setNotifyPositionListener(NotifyPositionListener notifyPositionListener){
        this.mNotifyPositionListener=notifyPositionListener;
    }

    public interface NotifyPositionListener{
        void setPosition(int position);
    }
}
